package com.whmnx.my.ui.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.bean.MoneyBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.UItils;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.ui.fragment.MyGoTiXianFragment;
import com.whmnx.my.ui.fragment.MyWithdrawFragment;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/24 0024 17:32
 * version: 1.0
 * @author Administrator
 */
public class WithdrawModel extends ToolbarViewModel {
    public ObservableField<String> Balance = new ObservableField<>("");

    MyWithdrawFragment mFragment;

    private Disposable subscribe;

    public WithdrawModel(@NonNull Application application) {
        super(application);


    }


    public void initToolbar(MyWithdrawFragment myWithdrawFragment) {
        this.mFragment = myWithdrawFragment;
        setTitleText("可提现金额");
        initView();
        initListener();
    }

    private void initListener() {
        subscribe = RxBus.getDefault().toObservable(Object.class)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object object){
                        if (object.equals(EventConstants.TIXIAN)){
                            initView();
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        params.put("userInfoId", SPUtils.getInstance().getString(Constants.USERID));
        params.put("pageIndex",1);
        params.put("moneyDetail_Type",0);
        params.put("pageSize",50);
        HttpClient.post(mFragment.getActivity(), Constants.MONEYFLOWINGLIST, params,new CallBack<MoneyBean>() {
            @Override
            public void onSuccess(MoneyBean moneyBean) {
                observableList.clear();
                double balance = moneyBean.getBalance();
                SPUtils.getInstance().put("balance", UItils.toBouble(balance) + "");
                Balance.set(UItils.toBouble(balance));
                for (MoneyBean.FlowingListModelBean flowingListModelBean : moneyBean.getFlowingListModel()) {
                    observableList.add(new WithdrawItemViewModel3(WithdrawModel.this,flowingListModelBean));
                }
            }
        });
    }


    //给RecyclerView添加ObservableList
    public ObservableList<WithdrawItemViewModel3> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<WithdrawItemViewModel3> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_withdraw);

    //去提现
    public BindingCommand goTixianOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            int lv = SPUtils.getInstance().getInt(Constants.LV);
            if (lv != 2){
                ToastUtil.toastShortMessage("请先去V2认证");
                return;
            }
            SPUtils.getInstance().put("type", 0);
            startContainerActivity(MyGoTiXianFragment.class.getCanonicalName());
        }
    });

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxSubscriptions.remove(subscribe);
    }
}
